package presentation.actions.oCompensationSys;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Collections;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import utils.formbeans.ToStringComparator;
import utils.actions.NavigationFunctions;
import utils.actions.BOAdderFunctions;
import utils.formbeans.FormBeanManager;
import utils.formbeans.BOAdderFormBean;
import utils.userUtils.UserUtils;
import utils.Pair;
import utils.listFilter.ViewListConfiguration;
import utils.beanUtils.ExtendedBeanUtils;
import utils.beanUtils.converters.NumberConverter;
import utils.filter.ListConfigurator;

import bussineslogic.controlers.UseCaseFacade;

import bussineslogic.objects.*;
import presentation.formbeans.objects.*;

import presentation.formbeans.oCompensationSys.*;


/**
 * 
 * @author Automatika - JustInMind SL
 *
 */
public class Action_new_new_page_compensation extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
    throws Exception {    	    	    

        /** 1.  We obtain the user Information */
        String usercode = UserUtils.getCurrentUsuarioId(request); 
        Locale locale = UserUtils.getCurrentLocale(request); 




        /** 2.  We obtain the initial form bean and we put it to a new FormBeanManager. */

        Action_new_new_page_compensation_Form action_new_new_page_compensation_Form=(Action_new_new_page_compensation_Form) form;

        FormBeanManager fbManager=new FormBeanManager(request, action_new_new_page_compensation_Form);	


        Compensation_Form compensationForm=action_new_new_page_compensation_Form.getCompensation_Form();
        Compensation compensationDTO=(Compensation) fbManager.getPOJO("compensation_Form",Compensation.class);
        Compensation lastCompensation=UseCaseFacade.ObtainCurrentCompensationFromPersonal(usercode, compensationForm.getCompensation_personal().getPersonalcode());

        NumberConverter conv=new NumberConverter(locale);
        if(lastCompensation!=null && compensationForm.getAmountType().equals(Compensation_Form.INCREASE_AMOUNT)){
            BigDecimal amountToAdd=(BigDecimal) conv.convert(BigDecimal.class, compensationForm.getAmountAugment());
            compensationDTO.setAmount(lastCompensation.getAmount().add(amountToAdd));
        }
        else if(lastCompensation!=null && compensationForm.getAmountType().equals(Compensation_Form.PERCENT_AMOUNT)){
            BigDecimal amountToAdd=(BigDecimal) conv.convert(BigDecimal.class, compensationForm.getPercentAugment());
            compensationDTO.setAmount(lastCompensation.getAmount().multiply(amountToAdd.add(new BigDecimal(100))).divide(new BigDecimal(100)));
        }

        /** 3.  We use the business logic to create the new item */

        Compensation compensation = UseCaseFacade.CreateCompensation(usercode,compensationDTO);	

        /** 4.  In case that a BOAdder exists in the previous jsp, we update the list of values */



        /** 5.  We clean the current container */
        fbManager.cleanContainer();

        /** 6.  We return to the correct page (introducing some values to the request if necessary) */


        /** 6.1.  We look if the current page is beeing opened in a popup window. */
        boolean isPopUp = NavigationFunctions.isPopUpWindow(request);
        /** 6.2.  We look for the forward to execute (we put the current object in case it is used in the next page) */

        //return NavigationFunctions.findForward(request, mapping, "success", "compensationcode="+compensation.getCompensationcode(), isPopUp, compensation.getCode(), compensation.toString());
        return new ActionForward("/oCompensationSys/popUp_action_mark_current_confirmation.do?compensationcode=" + compensation.getCode() + "&isPopUpWindow=true", true);

    }
}